<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsInsuranceController extends PublicController{
	 public function index(){
		 $this->display();
	 }

	 //获取保险种类
	 public function get_goods_insuran(){
	    $limit = I('get.limit');
        $page = I('get.page');
        $like = I('get.like');
        if(!empty($like)){
           $where['b.name|a.is_name|a.company']=array("like","%".$like."%");
         }
        $ret['total'] = M('goods_insurancelist')->count();
        $ret['data'] = M('goods_insurancelist')->alias('a')
        ->join('__INSURANCE_TYPE__ as b on a.insurance_type_id=b.id')
        ->field('a.id,a.is_name,a.company,a.content,a.logo,a.imgs,a.contact_name,a.contact_moblie,a.insurance_type_id,b.name')
        ->where($where)
        ->page($page,$limit)
        ->select();
        foreach ($ret['data'] as $key => $value) {
             $ret['data'][$key]['logo'] = '<img style="width:50px;" src="'.$ret['data'][$key]['logo'].'">';
           }
        
        $ret['code'] = 200;
        ret($ret);
	 }

	//添加保险展示页面
	 public function add(){
	 	$istype=M('insurance_type')->select();
	 	$this->istype=$istype;
	 	$this->display();
	 }
	 //添加提交
	 public function insert(){
	 	$data['is_name']=I('post.isname');
	 	$data['company']=I('post.company');
	 	$data['content']=I('post.content');
	 	$data['logo']=I('post.logo');
	 	$data['imgs']=json_encode($_POST['images']);
	 	$data['contact_name']=I('post.contact_name');
	 	$data['contact_moblie']=I('post.contact_moblie');
	 	$data['insurance_type_id']=I('post.istypeid');
	 	$re=M('goods_insurancelist')->add($data);
	 	if($re){
	 		$ret['isSuccess']=1;
	 		$ret['msg']="添加成功";
	 		ret($ret);
	 	}else{
	 		$ret['isSuccess']=1;
	 		$ret['msg']="添加失败";
	 		ret($ret);
	 	}
	 }

	 //编辑页面数据详情
	 public function edit(){
	 	$id=I('get.id');
	 	$isgoods=M('goods_insurancelist')->where(['id'=>$id])->find();
	 	$istype=M('insurance_type')->select();
	 	foreach ($istype as $key => $value) {
	 		if($istype[$key]['id']==$isgoods['insurance_type_id']){
	 			$istype[$key]['selected']=1;
	 		}else{
	 			$istype[$key]['selected']=0;
	 		}
	 	}
	 	$imgs=json_decode($isgoods['imgs']);
	 	$images=array();
	 	foreach ($imgs as $key => $value) {
	        $iii['imgs_src'] = $imgs[$key];
	        $iii['imgs_resrc'] = str_replace('/', '',$imgs[$key]);
	        $iii['imgs_resrc'] = str_replace('.', '',$iii['imgs_resrc']);
	        $iii['imgs_resrc'] = str_replace('_', '',$iii['imgs_resrc']);
	        array_push($images,$iii); //组成新数组保存地址路径跟修改后的地址路径，用于前台图片展示与删除
	      }
	 	$this->isgoods=$isgoods;
	 	$this->istype=$istype;
	 	$this->images=$images;
	 	$this->display();
	 }

	 public function save(){
	 	$data['id']=I('post.id');
	 	$data['is_name']=I('post.isname');
	 	$data['company']=I('post.company');
	 	$data['content']=I('post.content');
	 	$data['logo']=I('post.logo');
	 	$data['imgs']=json_encode($_POST['images']);
	 	$data['contact_name']=I('post.contact_name');
	 	$data['contact_moblie']=I('post.contact_moblie');
	 	$data['insurance_type_id']=I('post.istypeid');
	 	$re=M('goods_insurancelist')->where(['id'=>$data['id']])->save($data);
	 	if($re!==false){
	 		$ret['isSuccess']=1;
	 		$ret['msg']="修改成功";
	 		ret($ret);
	 	}else{
	 		$ret['isSuccess']=1;
	 		$ret['msg']="修改失败";
	 		ret($ret);
	 	}
	 }
	//单图上传
    public function car_ico(){
      $name = date("Ymd_His_",time()).rand(100000,999999);
      copy($_FILES['file']['tmp_name'],'Public/upload/insurance/logo/'.$name.'.jpg');
      $ret['isSuccess'] = 1;
      $ret['msg'] = "上传成功";
      $ret['src'] = '/Public/upload/insurance/logo/'.$name.'.jpg';
      ret($ret);
    }

    //多图上传
    public function uploadImage(){
      //ret($_FILES);exit();
      $name = date("Ymd_His_",time()).rand(100000,999999);
          copy($_FILES['file']['tmp_name'],'Public/upload/insurance/imgs/'.$name.'.jpg');
      $ret['isSuccess'] = 1;
      $ret['msg'] = '上传成功！';
      $ret['data']['src'] = '/Public/upload/insurance/imgs/'.$name.'.jpg';
      ret($ret);
	}

	
}
